/*
 * topological-hex, a program to compute combinatorial hexahedral meshes.
 *
 * Copyright (C) <2018> <Université catholique de Louvain (UCL), Belgique>
 *
 * List of the contributors to the development, description and complete
 * License: see LICENSE file.
 *
 * This program (topological-hex) is free software:
 * you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not,
 * see <http://www.gnu.org/licenses/>.
 */

#include <check.h>
#include <stdlib.h>

#include "types.h"

START_TEST(test_diagonal_index) {
  for (uint32_t i = 0; i < 1024; i++) {
    uint32_t n = (rand() % 50) + 2;
    uint32_t a = rand() % n;
    uint32_t b;
    do { b = rand() % n; } while (a == b);

    ck_assert_msg(diagonal_index(n, a, b) < n*n,
                  "Index of pair (%u, %u) mapped to %u for set of %u vertices, "
                  "when the maximum value should be %u",
                  a, b, diagonal_index(n, a, b), n*n);
    ck_assert_int_eq(diagonal_index(n, a, b), diagonal_index(n, b, a));
  }
} END_TEST

START_TEST(test_hex_facet) {
  for (uint32_t i = 0; i < 1024; i++) {
    hex_index h = rand() % 256;
    hex_facet_index f = rand() % 6;

    hex_facet desc = make_hex_facet(h, f);
    ck_assert_int_eq(hex_facet_hex(desc), h);
    ck_assert_int_eq(hex_facet_facet(desc), f);
  }
} END_TEST

START_TEST(test_quad_descriptor) {
  for (uint32_t i = 0; i < 1024; i++) {
    quad_index q = rand() % 256;
    hex_index h = rand() % 256;
    vertex_index a = rand() % 256;
    vertex_index b;
    do { b = rand() % 256; } while (a == b);

    quad_descriptor desc = make_quad_descriptor(q, h, a, b);
    ck_assert_int_eq(quad_descriptor_quad_index(desc), q);
    ck_assert_int_eq(quad_descriptor_hex_index(desc), h);
    ck_assert_int_eq(quad_descriptor_first_vertex(desc), a);
    ck_assert_int_eq(quad_descriptor_second_vertex(desc), b);
  }
} END_TEST

START_TEST(test_quad_descriptor_no_hex) {
  for (uint32_t i = 0; i < 1024; i++) {
    quad_index q = rand() % 256;
    hex_index h = NO_HEXAHEDRON;
    vertex_index a = rand() % 256;
    vertex_index b;
    do { b = rand() % 256; } while (a == b);

    quad_descriptor desc = make_quad_descriptor(q, h, a, b);
    ck_assert_int_eq(quad_descriptor_quad_index(desc), q);
    ck_assert_int_eq(quad_descriptor_hex_index(desc), h);
    ck_assert_int_eq(quad_descriptor_first_vertex(desc), a);
    ck_assert_int_eq(quad_descriptor_second_vertex(desc), b);
  }
} END_TEST

START_TEST(test_quad_edge) {
  for (uint32_t i = 0; i < 1024; i++) {
    quad_index q = rand() % 256;
    quad_edge_index e = rand() % 4;

    quad_edge desc = make_quad_edge(q, e);
    ck_assert_int_eq(quad_edge_quad(desc), q);
    ck_assert_int_eq(quad_edge_edge(desc), e);

    ck_assert_int_eq(quad_edge_quad(next_edge(desc)), q);
    ck_assert_int_eq(quad_edge_quad(prev_edge(desc)), q);
    ck_assert_int_eq(quad_edge_quad(opposite_edge(desc)), q);

    ck_assert_int_eq(quad_edge_edge(next_edge(desc)), (e + 1) % 4);
    ck_assert_int_eq(quad_edge_edge(prev_edge(desc)), (e + 3) % 4);
    ck_assert_int_eq(quad_edge_edge(opposite_edge(desc)), (e + 2) % 4);
  }
} END_TEST

Suite *types_suite(void) {
  Suite *s = suite_create("Types");

  TCase *tcase = tcase_create("Diagonal index");
  tcase_add_test(tcase, test_diagonal_index);
  suite_add_tcase(s, tcase);

  tcase = tcase_create("Descriptors");
  tcase_add_test(tcase, test_hex_facet);
  tcase_add_test(tcase, test_quad_descriptor);
  tcase_add_test(tcase, test_quad_descriptor_no_hex);
  suite_add_tcase(s, tcase);

  tcase = tcase_create("Quad edge");
  tcase_add_test(tcase, test_quad_edge);
  suite_add_tcase(s, tcase);

  return s;
}
